﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using TCS.DBUtility;
namespace TCS.DAL
{
    /// <summary>
    /// 数据访问类Gsp_Administrative。
    /// </summary>
    public class Gsp_Administrative
    {
        public Gsp_Administrative()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("AddID", "Gsp_Administrative");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int AddID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Gsp_Administrative");
            strSql.Append(" where AddID=@AddID ");
            SqlParameter[] parameters = {
					new SqlParameter("@AddID", SqlDbType.Int,4)};
            parameters[0].Value = AddID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(TCS.Model.Gsp_Administrative model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Gsp_Administrative(");
            strSql.Append("AddName,Remark)");
            strSql.Append(" values (");
            strSql.Append("@AddName,@Remark)");
            SqlParameter[] parameters = {
					new SqlParameter("@AddName", SqlDbType.NVarChar,50),
					new SqlParameter("@Remark", SqlDbType.NVarChar,50)};

            parameters[0].Value = model.AddName;
            parameters[1].Value = model.Remark;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void AddNew(string AddName, string Remark, int ParentID, string Temp, string Depth)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CInfo_Administrative(");
            strSql.Append("AddName,Remark,ParentID,Temp,Depth)");
            strSql.Append(" values (");
            strSql.Append("@AddName,@Remark,@ParentID,@Temp,@Depth)");
            SqlParameter[] parameters = {
					new SqlParameter("@AddName", SqlDbType.NVarChar,50),
					new SqlParameter("@Remark", SqlDbType.NVarChar,50),
                     new SqlParameter("@ParentID", SqlDbType.Int),
                     new SqlParameter("@Temp", SqlDbType.NVarChar,200),
                     new SqlParameter("@Depth", SqlDbType.Int)
                                        };

            parameters[0].Value = AddName;
            parameters[1].Value = Remark;
            parameters[2].Value = ParentID;
            parameters[3].Value = Temp;
            parameters[4].Value = Depth;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(TCS.Model.Gsp_Administrative model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Gsp_Administrative set ");
            strSql.Append("AddName=@AddName,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where AddID=@AddID ");
            SqlParameter[] parameters = {
					new SqlParameter("@AddID", SqlDbType.Int,4),
					new SqlParameter("@AddName", SqlDbType.NVarChar,50),
					new SqlParameter("@Remark", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.AddID;
            parameters[1].Value = model.AddName;
            parameters[2].Value = model.Remark;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int AddID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Gsp_Administrative ");
            strSql.Append(" where AddID=@AddID ");
            SqlParameter[] parameters = {
					new SqlParameter("@AddID", SqlDbType.Int,4)};
            parameters[0].Value = AddID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TCS.Model.Gsp_Administrative GetModel(int AddID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from CInfo_Administrative ");
            strSql.Append(" where AddID=@AddID ");
            SqlParameter[] parameters = {
					new SqlParameter("@AddID", SqlDbType.Int,4)};
            parameters[0].Value = AddID;

            TCS.Model.Gsp_Administrative model = new TCS.Model.Gsp_Administrative();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["AddID"].ToString() != "")
                {
                    model.AddID = int.Parse(ds.Tables[0].Rows[0]["AddID"].ToString());
                }
                model.AddName = ds.Tables[0].Rows[0]["AddName"].ToString();
                model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                model.Depth = Convert.ToInt32(ds.Tables[0].Rows[0]["Depth"]);
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM CInfo_Administrative ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM CInfo_Administrative ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetListNew(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM CInfo_Administrative ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Gsp_Administrative";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

